﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.field
{
    class AD
    {
        //adress
        private StringBuilder street_address;
        private StringBuilder other_designation;
        private StringBuilder city;
        private StringBuilder state;
        private StringBuilder zip_code;

        public StringBuilder street_Address { get { return street_address; } set { street_address = value; } }
        public StringBuilder Other_designation { get { return other_designation; } set { other_designation = value; } }
        public StringBuilder City { get { return city; } set { city = value; } }
        public StringBuilder State { get { return state; } set { state = value; } }
        public StringBuilder Zip_code { get { return zip_code; } set { zip_code = value; } }

        public AD()
        {
            street_address = new StringBuilder();
            other_designation = new StringBuilder();
            city = new StringBuilder();
            state = new StringBuilder();
            zip_code = new StringBuilder();
        }
        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    street_address.Capacity = street_address.Length;
                    other_designation.Capacity = other_designation.Length;
                    city.Capacity = city.Length;
                    state.Capacity = state.Length;
                    zip_code.Capacity = zip_code.Length;
                    return i-1;
                }
                else if (source[i] == '^')
                    x++;
                else if (source[i] == '|')
                {
                    street_address.Capacity = street_address.Length;
                    other_designation.Capacity = other_designation.Length;
                    city.Capacity = city.Length;
                    state.Capacity = state.Length;
                    zip_code.Capacity = zip_code.Length;
                    return i;
                }

                else if (x == 0)
                {
                  
                    street_address.Insert(street_address.Length, source[i]);
                }
                else if (x == 1)
                {

                    other_designation.Insert(other_designation.Length, source[i]);
                }
                else if (x == 2)
                {

                    city.Insert(city.Length, source[i]);
                }
                else if (x == 3)
                {

                    state.Insert(state.Length, source[i]);
                }
                else if (x == 4)
                {

                    zip_code.Insert(zip_code.Length, source[i]);
                }
            }
            return i;
        }

        public string tostring()
        {
            string hd_string = "";

            if (street_address .Length!= 0)
                hd_string += street_address;
            if (other_designation.Length != 0)
                hd_string += "^" + other_designation;
            if (city.Length != 0l)
                hd_string += "^" + city;
            if (state.Length != 0)
                hd_string += "^" + state;
            if (zip_code.Length != 0)
                hd_string += "^" + zip_code;
            return hd_string;
        }

    }
}
